package com.authine.cloudpivot.ext.applicationservice;

import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.cola.exception.BizException;
import com.authine.cloudpivot.ext.constants.PlatformConst;
import com.authine.cloudpivot.ext.dto.transfer.ExportVendorPayAbleDto;

import com.authine.cloudpivot.ext.enums.report.ReportType;
import com.authine.cloudpivot.ext.service.ExportReportService;
import com.authine.cloudpivot.ext.service.impl.ExportReportServiceImpl;
import com.authine.cloudpivot.ext.utils.system.ReportUtils;
import com.authine.hermes.app.launcher.service.ApplicationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import static com.authine.hermes.app.launcher.service.ApplicationServiceFoundation.CUSTOM_SERVICE;

@Path("ExportReportController")
@Slf4j
public class ExportReportController extends ApplicationService {
    private ExportReportService exportReportService = new ExportReportServiceImpl();

    @Path("exportVendorPayAble")
    @POST
    @Operation(summary = "转采财务/采购付款/导出供应商应付报表", tags = CUSTOM_SERVICE)
    public SingleResponse exportVendorPayAble(@Parameter ExportVendorPayAbleDto exportVendorPayAbleDto) {

        log.info("exportVendorPayAble start request param:{}", exportVendorPayAbleDto);
        if (StringUtils.isEmpty(exportVendorPayAbleDto.getType())) {
            throw new BizException("报表导出类型不正确");
        }

        ReportType reportType = ReportType.get(exportVendorPayAbleDto.getType().toLowerCase());

        if (reportType == null) {
            return SingleResponse.buildFailure("-1", "请求参数fileType不能为空");
        }

        List<Map<String, Object>> reportList = exportReportService.vendorAccountPayableReport(exportVendorPayAbleDto);

        if (CollectionUtils.isEmpty(reportList)) {
            return SingleResponse.buildFailure("-1", "未获取到下载数据");
        }

        log.info("exportVendorPayAble export data :{}", reportList);
        for (Map<String, Object> map : reportList) {

            if (null == map.get("payment") && null != map.get("invoiceAmount")) {
                //收票不为空，付款为空
                map.put("payment", BigDecimal.ZERO.doubleValue());
                map.put("noPayment", null == map.get("purchaseAmount") ? null : new BigDecimal(String.valueOf(map.get("purchaseAmount"))).doubleValue());
                map.put("noInvoice", BigDecimal.ZERO.subtract(new BigDecimal(String.valueOf(map.get("invoiceAmount")))).doubleValue());
                map.put("balancePayable", new BigDecimal(String.valueOf(map.get("invoiceAmount"))).subtract(BigDecimal.ZERO).doubleValue());
            } else if (null != map.get("payment") && null == map.get("invoiceAmount")) {
                //付款不为空，收票为空
                map.put("invoiceAmount", BigDecimal.ZERO.doubleValue());
                map.put("noInvoice", new BigDecimal(String.valueOf(map.get("payment"))).subtract(BigDecimal.ZERO).doubleValue());
                map.put("noPayment", new BigDecimal(String.valueOf(map.get("purchaseAmount"))).subtract(new BigDecimal(String.valueOf(map.get("payment")))).doubleValue());
                map.put("balancePayable", BigDecimal.ZERO.subtract(new BigDecimal(String.valueOf(map.get("payment")))).doubleValue());

            } else if (null != map.get("payment") && null != map.get("invoiceAmount")) {
                //付款收票都不为空
                map.put("noPayment", new BigDecimal(String.valueOf(map.get("purchaseAmount"))).subtract(new BigDecimal(String.valueOf(map.get("payment")))).doubleValue());
                map.put("noInvoice", new BigDecimal(String.valueOf(map.get("payment"))).subtract(new BigDecimal(String.valueOf(map.get("invoiceAmount")))).doubleValue());
                map.put("balancePayable", new BigDecimal(String.valueOf(map.get("invoiceAmount"))).subtract(new BigDecimal(String.valueOf(map.get("payment")))).doubleValue());
            }

            if (map.get("platformName") != null) {
                String platformName = PlatformConst.PLATFORM_ORGANIZATIONS.get(map.get("platformName"));
                map.put("platformName",platformName);
            }
        }

        log.info("exportVendorPayAble end");
        return ReportUtils.exportReport("vendor_account_payable_report", reportType.getId(), reportList);

    }

}
